package releasemanager.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.team.svn.ui.SVNTeamUIPlugin;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.team.svn.ui.repository.RepositoriesView;
import org.eclipse.team.svn.ui.repository.RepositoryTreeViewer;
import org.eclipse.team.svn.ui.repository.model.RepositoryResource;
import org.eclipse.team.svn.core.SVNTeamPlugin;
import org.eclipse.team.svn.core.svnstorage.SVNRemoteStorage;
import org.eclipse.team.svn.ui.repository.model.RepositoryLocation;

public class RmHandler extends AbstractHandler {
	
	protected RepositoryResource inputElement;
	
	public RmHandler() {
	}
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		String viewsStr =  null;
		for(int i=0;i<HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getViews().length;i++){
			viewsStr += HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getViews()[i].getViewSite().getId()+"/";
		}
		
		//System.out.println(SVNTeamUIPlugin.instance().getWorkbench().getViewRegistry().getViews().length);
		System.out.println(SVNRemoteStorage.instance().getRepositoryLocations().length);
		//SVNTeamUIPlugin.instance().getWorkbench().getActiveWorkbenchWindow().getActivePage()
		//SVNRemoteStorage.instance().getRepositoryLocations().length;
		String strSvn = Integer.toString(SVNRemoteStorage.instance().getRepositoryLocations().length);
		
		for(int i=0;i<SVNRemoteStorage.instance().getRepositoryLocations().length;i++){
			strSvn += SVNRemoteStorage.instance().getRepositoryLocations()[i].getLabel()+" / \n";
		}
		
		
		
		
		//RepositoriesView view = RepositoriesView.instance();
		//RepositoryTreeViewer treeViewer = view.getRepositoryTree();
		//view.getRepositoryTree().getTree().getItems()
		
		MessageDialog.openInformation(
				window.getShell(),
				"ReleaseManager",
				strSvn);
		
		try {
			//HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView("releasemanager.views.BrowserView");
			if(HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView("releasemanager.views.BrowserView") != null){			
				HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().hideView(
					HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView("releasemanager.views.BrowserView"));
			}else{
				HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView("releasemanager.views.BrowserView");
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
